#include "all_drive.h"
#include <QDebug>
#include <QObject>
#include <QFile>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include <fcntl.h>                          // 提供open()函数
#include <sys/types.h>                      // 提供mode_t类型
#include <sys/stat.h>                       // 提供open()函数的符号


#define GEC6818_ADC_IN0		_IOR('A',  1, unsigned long)
#define GEC6818_ADC_IN1		_IOR('A',  2, unsigned long)

extern int kbdfd,ledsfd,beepfd,adcfd;

all_drive::all_drive()
{

}

void all_drive::startinit()
{
    kbdfd=open("/dev/x6818-kbd", O_RDWR|O_NONBLOCK);
    if (kbdfd < 0) {//打开设备失败，电脑端或为加载驱a动
           Messages= QObject::tr("kbd open fail!");
           qDebug()<<"kbd open fail!";
    }
    else{
        Messages= QObject::tr("kbd open succeed");
        qDebug()<<"kbd open succeed!";
    }
    ledsfd=open("/dev/x6818-leds", O_RDWR|O_NONBLOCK);
    if (ledsfd < 0) {
            Messages = Messages + QObject::tr("LED open fail!");
             qDebug()<<"LED open fail!";
    }
    else{
        Messages= Messages + QObject::tr("LED open succeed");
        qDebug()<<"LED open succeed!";
        for(int d=0;d<4;d++)
            LEDsSetValue(1,d);
        }

    beepfd=open("/dev/x6818-beep", O_RDWR|O_NONBLOCK);
    if (beepfd < 0) {//打开设备失败，电脑端或为加载驱a动
           Messages= Messages + QObject::tr("beep open fail!");
    }
    else{
        Messages= Messages + QObject::tr("beep open succeed");
    }

    adcfd=open("/dev/adc_drv", O_RDWR|O_NONBLOCK);
    if (adcfd < 0) {//打开设备失败，电脑端或为加载驱a动
           Messages= Messages + QObject::tr("adc open fail!");
    }
    else{
        Messages= Messages + QObject::tr("adc open succeed");
    }

    uart4 = new QSerialPort();
    if(uart4->isOpen())
    {
        uart4->clear();
        uart4->close();
    }
    QStringList serialname;
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
        serialname << info.portName();
        qDebug()<<"serialPortName:"<<info.portName();
    }
    uart4->setPortName(serialname[7]);
    if(!uart4->open(QIODevice::ReadWrite))
    {
        qDebug()<<serialname[7]<<" open failed";
    }
    else
    {
        uart4->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);//设置波特率和读写方向
        uart4->setDataBits(QSerialPort::Data8);		//数据位为8位
        uart4->setFlowControl(QSerialPort::NoFlowControl);//无流控制
        uart4->setParity(QSerialPort::NoParity);	//无校验位
        uart4->setStopBits(QSerialPort::OneStop); //一位停止位
        connect(uart4,&QSerialPort::readyRead,this,&all_drive::uart4_callback);
    }

}

void all_drive::adcScan() //adc采样服务程序
{
    rt=ioctl(adcfd,GEC6818_ADC_IN0,&adc_vol);
    qDebug()<<"adc value is "<<adc_vol;
    //printf("adc in0 vol: %lu=mv\r\n",adc_vol);

}

void all_drive::KbdLedScan()//按键LED状态扫扫描函数
{
    qDebug()<<"KbdLedScan is ok";
 /*if(!(kbdfd<0))//按键驱动可用，读取按键状态
 {
     read(kbdfd,&KbdStatus,1);
     if(Last_key_value == KbdStatus)  //按一下触发一次
     {
         return;
     }

     switch (KbdStatus)
     {    if(ledsfd > 0)//如果设备驱动正常
        ioctl(ledsfd,value,ledsnum);
         case 1:   //第一个按键
             key_arr[0] =(key_arr[0] +1)%2 ;
             break;
         case 2:   //第二个按键
             key_arr[1] =(key_arr[1] +1)%2 ;
             break;
         case 4:   //第三个按键
             key_arr[2] =(key_arr[2] +1)%2 ;
             break;
         case 8:  //第四个按键
             key_arr[3] =(key_arr[3] +1)%2 ;
             break;
         default:
             Last_key_value = 0;
             break;
     }
 }
*/
}


//leds控制函数
void all_drive::LEDsSetValue(char value,int ledsnum)
{
    if(value==1)
    {
        led_status^=1<<ledsnum;
    }
    else {
        led_status&=~(1<<ledsnum);
    }
    write(ledsfd,&led_status,1);
    qDebug()<<"status:"<<(int)led_status;

}

void all_drive::close_all()
{
    if(kbdfd>0)
        close(kbdfd);
    if(ledsfd>0)
        close(ledsfd);
    if(beepfd>0)
        close(beepfd);
    if(adcfd>0)
        close(adcfd);

}

void all_drive::uart4_callback()
{
    QByteArray uart4_data = uart4->readAll();

    qDebug()<<"receive info:"<<uart4_data;

}
